package com.zxg.web.controller;

import com.zxg.web.util.BatchExportToExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhaoxiaoguang
 * @Description:
 * @Date: created in 2018/5/15
 * @Modidied By:
 **/
@Controller
@RequestMapping("/login")
public class LoginController {


    @RequestMapping("/index1.do")
    public String index(){
        return "login";
    }

    @RequestMapping(value = "/downLoad.do",method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public void downLoad(HttpServletResponse response){

        /**
         * 测试代码
         */
        String[] headers = { "序列号","会员id",  "姓名", "年龄"};
        String[] columns = { "serialId","customerId", "name","age"};
        String title = "测试代码";
        Workbook workbook = BatchExportToExcelUtil.createWorkBook(title,headers);
        List<Person> list = new ArrayList();
        Person person = new Person();
        person.setCustomerId("12345");
        person.setName("zhaoxiaoguang");
        list.add(person);
        try {
            workbook = BatchExportToExcelUtil.exportExcel(workbook, columns, list);
        } catch (Exception e) {
        }
        response.setContentType("application/vnd.ms-excel");
        //response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String((title + ".xlsx").getBytes(), "iso-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            ServletOutputStream out =response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
